define(['./bslCameraOption'],function(bslCameraOption){

    var channel = cordova.require('cordova/channel');

    channel.createSticky('onCordovaInfoReady');
    // Tell cordova channel to wait on the CordovaInfoReady event
    channel.waitForInitialization('onCordovaInfoReady');

    var getCameraError = function(error){
        var result = error;
        if (result.code == CaptureError.CAPTURE_INTERNAL_ERR) {
            result.message = "The camera or microphone failed to capture image or sound.";
        }

        else if (result.code == CaptureError.CAPTURE_APPLICATION_BUSY){
            result.message = "The camera or audio capture application is currently serving another capture request.";
        }

        else if (result.code == CaptureError.CAPTURE_INVALID_ARGUMENT){
            result.message = "Invalid use of the API (e.g., the value of limit is less than one).";
        }

        else if (result.code == CaptureError.CAPTURE_NO_MEDIA_FILES){
            result.message = " The user exits the camera or audio capture application before capturing anything.";
        }

        else if (result.code == CaptureError.CAPTURE_NOT_SUPPORTED){
            result.message = "The requested capture operation is not supported.";
        }

        return result;
    };

    var getCameraSucPath = function(mediaFiles){
        var i, path, len;
        for (i = 0, len = mediaFiles.length; i < len; i += 1){
          path = mediaFiles[i].fullPath;
        }
        return path;
    };


    var cmr = {
        description: "访问摄像头设备",
        // cameraDirection: null,//前置or后置摄像
        supportedImageResolutions: null,//拍照分辨率，ios下空数组
        supportedVideoResolutions: null,//摄像分辨率，ios下空数组
        supportedImageFormats: null,//拍照文件格式，ios下空数组
        supportedVideoFormats: null,//摄像文件格式，ios下空数组


        
        //录像函数
        startVideoCapture:function(successCB, errorCB, option){
            navigator.device.capture.captureVideo(cameraSuccess, cameraError, option);

            function cameraSuccess(mediaFiles){
              var path = getCameraSucPath(mediaFiles);
              successCB(path);
            };

            function cameraError(error){
                var result = getCameraError(error);
                errorCB(result);
            };
        },

        stopVideoCapture: function(){
            cordova.exec(function(){
                console.log("录像停止成功");
            },function(message){
                console.log("录像停止失败:" + message);
            },'Capture','stopVideo',[]);
        },

        //拍照函数
        captureImage: function(successCB, errorCB, option){
            var me = this;
            var op = bslCameraOption.createOption(option);//初始化拍照参数
            //navigator.device.capture.captureImage();
            navigator.camera.getPicture(cameraSuccess,cameraError,op);

            //拍照成功回调
            function cameraSuccess(imageURI) {
                //imageURI 图片或视频路径
                successCB(imageURI);
            }
          
            //拍照失败回调
            function cameraError(message) {
                var error = new Object();
                error.code = null;//暂时 还没有错误编码
                error.message = message;
                errorCB(error);
            }

        },
    };

    channel.onCordovaReady.subscribe(function(){
        // cmr.cameraDirection = Camera.Direction.BACK;//前置or后置摄像
        cmr.supportedImageResolutions = navigator.device.capture.supportedImageModes;//拍照分辨率，ios下空数组
        cmr.supportedVideoResolutions = navigator.device.capture.supportedVideoModes;//摄像分辨率，ios下空数组
        cmr.supportedImageFormats = navigator.device.capture.supportedImageModes;//拍照文件格式，ios下空数组
        cmr.supportedVideoFormats = navigator.device.capture.supportedVideoModes;//摄像文件格式，ios下空数组
    });

    return cmr;
});
